package com.magicsoft.share.ui.share;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ArticleListOtherAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.share.OtherUserDetail;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.share.HomePagePresenter;
import com.magicsoft.share.utils.AppBarStateChangeListener;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.share.IHomePageView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

public class HomePageActivity extends BaseActivity<IHomePageView, HomePagePresenter> implements IHomePageView {
    @BindView(R.id.backdrop)
    ImageView backdrop;
    @BindView(R.id.tv_follow_num)
    TextView tvFollowNum;
    @BindView(R.id.layout_follow)
    LinearLayout layoutFollow;
    @BindView(R.id.tv_fans_num)
    TextView tvFansNum;
    @BindView(R.id.layout_fans)
    LinearLayout layoutFans;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_diamonds_golden)
    ImageView ivDiamondsGolden;
    @BindView(R.id.iv_head)
    RoundedImageView ivHead;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    @BindView(R.id.tv_follow)
    TextView tvFollow;
    @BindView(R.id.tv_chat)
    TextView tvChat;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.main_content)
    CoordinatorLayout mainContent;

    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;
    private ArticleListOtherAdapter articleListAdapter;
    private long appUserId;



    @Override
    public HomePagePresenter initPresenter() {
        return new HomePagePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_page;
    }

    @Override
    public void initData() {


        appUserId = getIntent().getIntExtra("appUserId", -1);
        Log.e("mArticle",appUserId+"");
    }

    @Override
    public void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.useDefaultLoadMore();
        //   recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNum++;
                requestData();
            }
        });


        articleListAdapter = new ArticleListOtherAdapter(R.layout.item_article_no_head);
        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.d("lyf", "onItemClick: " + position + recyclerView.getHeaderItemCount());
                ArticleList.ListBean item = articleListAdapter.getItem(position);
                Intent intent = new Intent(HomePageActivity.this, ArticleActivity.class);
                intent.putExtra("articleId", item.getArticleId());
                startActivityForResult(intent, Constant.REFRESH_UI);
            }
        });

        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(this, 10), BaseQuickAdapter.HEADER_VIEW));


        recyclerView.setAdapter(articleListAdapter);

        appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    //展开状态
                    tvTitle.setVisibility(View.GONE);

                } else if (state == State.COLLAPSED) {
                    //折叠状态
                    tvTitle.setVisibility(View.VISIBLE);
                } else {

                    //中间状态
                    tvTitle.setVisibility(View.GONE);
                }
            }
        });


    }

    @Override
    public void requestData() {


        mPresenter.seeUserDetail(appUserId);
        mPresenter.articleList(appUserId, pageNum, pageSize);
    }


    @OnClick({R.id.ibtn_left, R.id.tv_follow, R.id.tv_chat, R.id.iv_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_follow:
                if (tvFollow.getText().equals("取消关注")) {
                    mPresenter.deleteFollow(appUserId);
                } else {
                    mPresenter.addFollow(appUserId);
                }
                break;
            case R.id.tv_chat:
                break;
            case R.id.iv_head:
                startActivity(new Intent(this, DataOthersActivity.class));
                break;
        }
    }

    @Override
    public void seeUserDetailSuccess(OtherUserDetail data) {


        Glide.with(this).load(data.getHeadimg()).into(ivHead);
        Glide.with(this).load(data.getHeadimg())
                .apply(bitmapTransform(new BlurTransformation(22)))
                .into(backdrop);
        tvTitle.setText(data.getNickname());
        tvName.setText(data.getNickname());
        tvSign.setText(data.getPersonalized_signature());
        if (data.getMember() == 1) {
            ivDiamondsGolden.setVisibility(View.VISIBLE);
            tvId.setText("齐享号" + data.getQixiang_no());
        } else {
            ivDiamondsGolden.setVisibility(View.GONE);
        }
        tvFollowNum.setText(data.getFollow_num() + "");
        tvFansNum.setText(data.getFans_num() + "");
        if (data.isFollow_status()) {
            tvFollow.setText("取消关注");
        } else {
            tvFollow.setText("关注");
        }

    }

    @Override
    public void followChangeSuccess() {
        mPresenter.seeUserDetail(appUserId);
    }

    @Override
    public void ArticleListSuccess(ArticleList data) {

        if (isRefresh) {
            articleListAdapter.setNewData(data.getList());
        } else {
            articleListAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }

    }

    @Override
    public void ArticleListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }


}


